package cn.yunyichina.provider.cashpool.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;

import cn.yunyichina.provider.cashpool.dao.CouponCodeDao;
import cn.yunyichina.provider.cashpool.dao.CouponDao;
import cn.yunyichina.provider.cashpool.dao.CouponPushDao;
import cn.yunyichina.provider.cashpool.entity.CouponPush;
import cn.yunyichina.provider.cashpool.vo.CouponPushVo;
import cn.yunyichina.provider.framework.page.PageInfo;
import cn.yunyichina.provider.framework.utils.UUIDUtil;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.utils.commons.DateUtils;
import cn.yunyichina.utils.convert.JsonUtils;

@Service("couponpushServiceImpl")
public class CouponPushServiceImpl {
    private Logger logger = LoggerFactory.getLogger(CouponPushServiceImpl.class);
 
    @Autowired
    private CouponPushDao couponPushDao;

    @Autowired
    private CouponDao couponDao;

    @Autowired
    private CouponCodeDao couponCodeDao;

    /**
     * 获取优惠券推送列表 - yyt.cashpool.couponpush.list
     * 
     * @param json
     * @param sessionId
     * @return
     */
    public Response listCouponpush(String json, String sessionId) {
        logger.info("listCouponpush start json=" + json);
        Response response = new Response();

        try {
            CouponPushVo couponPushVo = JsonUtils.parseObject(json, CouponPushVo.class);
            List<String> orderColumns = new ArrayList<String>();
            orderColumns.add("create_time");

            PageInfo pageInfo = new PageInfo();
            pageInfo.setPageNum(couponPushVo.getPageNum());
            pageInfo.setPageSize(couponPushVo.getPageSize());

            Map<String, Object> map = new HashMap<String, Object>();

            CouponPush wheres = new CouponPush();
            if (StringUtils.isNotBlank(couponPushVo.getCouponCode())) {
                wheres.setCouponCode(couponPushVo.getCouponCode());
            }
            if (StringUtils.isNotBlank(couponPushVo.getPatArea())) {
                wheres.setPatArea(couponPushVo.getPatArea());
            }
            if (StringUtils.isNotBlank(couponPushVo.getPatName())) {
                wheres.setPatName(couponPushVo.getPatName());
            }
            if (StringUtils.isNotBlank(couponPushVo.getPatName())) {
                wheres.setPatName(couponPushVo.getPatName());
            }
            if (StringUtils.isNotBlank(couponPushVo.getCouponId())) {
                wheres.setCouponId(couponPushVo.getCouponId());
            }
            if (couponPushVo.getIsReceived() != null) {
                wheres.setIsReceived(couponPushVo.getIsReceived());
            }
            
            List<Map<String, Object>> datamaps = couponPushDao.selectByCondition(wheres, null, orderColumns, false, pageInfo);
            
            List<Map<String, Object>> remaps = new ArrayList<Map<String,Object>>();
            for (Map<String, Object> datamap : datamaps) {
                Map<String, Object> remap = new HashMap<String, Object>();
                remap.put("id", datamap.get("id"));
                remap.put("order_no", datamap.get("order_no"));
                remap.put("order_type", datamap.get("order_type"));
                remap.put("coupon_id", datamap.get("coupon_id"));
                remap.put("pat_name", datamap.get("pat_name"));
                remap.put("pat_age", datamap.get("pat_age"));
                remap.put("pat_sex", datamap.get("pat_sex"));
                remap.put("pat_mobile", datamap.get("pat_mobile"));
                remap.put("pat_area", datamap.get("pat_area"));
                remap.put("push_time", datamap.get("push_time"));
                remap.put("coupon_code", datamap.get("coupon_code"));
                remap.put("pat_area", datamap.get("pat_area"));
                remap.put("coupon_deadline_time", datamap.get("coupon_deadline_time"));
                remap.put("is_pushed", datamap.get("is_pushed"));
                remap.put("is_received", datamap.get("is_received"));
                remaps.add(remap);
            }
            
            map.put("couponPushs", remaps);

            long couponCodesCount = couponPushDao.selectByConditionCount(wheres);
            map.put("count", couponCodesCount);

            response.setResult(JsonUtils.toJsonString(map));
        } catch (Exception e) {
            logger.error("listCouponpush error", e);
            return Response.failure();
        }

        logger.info("listCouponpush response=" + JSON.toJSONString(response));
        return response;
    }

    /**
     * 添加优惠券推送信息 - yyt.cashpool.couponpush.add
     * 
     * @param json
     * @param sessionId
     * @return
     */
    public Response addCouponpush(String json, String sessionId) {
        logger.info("addCouponpush start json=" + json);
        try {
            CouponPushVo couponPushVo = JsonUtils.parseObject(json, CouponPushVo.class);
            CouponPush record = new CouponPush();
            BeanUtils.copyProperties(couponPushVo, record);

            record.setId(UUIDUtil.get32UUID());
            record.setPushTime(DateUtils.dateToString(new Date()));
            record.setCreateTime(DateUtils.dateToString(new Date()));
            couponPushDao.insert(record);
        } catch (Exception e) {
            logger.error("addCouponpush error", e);
            return Response.failure();
        }
        return Response.success();
    }

    /**
     * 用户领取优惠券，更新优惠券推送信息 - yyt.cashpool.couponpush.update
     * 
     * @param json
     * @param sessionId
     * @return
     */
    public Response updateCouponpush(String json, String sessionId) {
        logger.info("addCouponpush start json=" + json);
        try {
            CouponPushVo couponPushVo = JsonUtils.parseObject(json, CouponPushVo.class);
            CouponPush record = new CouponPush();
            BeanUtils.copyProperties(couponPushVo, record);

            record.setId(UUIDUtil.get32UUID());
            record.setPushTime(DateUtils.dateToString(new Date()));
            record.setCreateTime(DateUtils.dateToString(new Date()));
            couponPushDao.insert(record);
        } catch (Exception e) {
            logger.error("addCouponpush error", e);
            return Response.failure();
        }
        return Response.success();
    }

}
